package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.FriendApi;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.vo.ContactVo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MessagesService {
    @DubboReference
    private UserInfoApi userInfoApi;
    @DubboReference
    private UserApi userApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @DubboReference
    private FriendApi friendApi;

    public UserInfoVo findUserInfoByHuanxin(String huanxinId) {
        //1.根据环信id查询用户
        User user = userApi.findByHuanxin(huanxinId);
        //2.根据用户id查询用户详情
        UserInfo userInfo = userInfoApi.findById(user.getId());
        UserInfoVo vo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo,vo);
        if (userInfo.getAge() != null){
            vo.setAge(userInfo.getAge().toString());
        }
        return vo;
    }

    //添加好友
    public void contacts(Long userId) {
        //1.将好友关系注册到环信
        Boolean contact = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + UserHolder.getUserId(), Constants.HX_USER_PREFIX + userId);
        if (!contact){
            throw new BusinessException(ErrorResult.error());
        }
        //2.如果注册成功，记录好友关系到mongodb
        friendApi.save(UserHolder.getUserId(),userId);
    }
    //删除好友
    public void disContacts(Long userId) {
        Boolean disContact = huanXinTemplate.deleteContact(Constants.HX_USER_PREFIX + UserHolder.getUserId(),Constants.HX_USER_PREFIX + userId);
        if (!disContact){
            throw new BusinessException(ErrorResult.error());
        }
        friendApi.delete(UserHolder.getUserId(),userId);
    }

    /**
     * 分页查询联系人列表
     * @param page
     * @param pagesize
     * @param keyword
     * @return
     */
    public PageResult findFriends(Integer page, Integer pagesize, String keyword) {
        //1.调用Api查询当前用户的好友数据
        List<Friend> list = friendApi.findByUserId(UserHolder.getUserId(),page,pagesize);
        if (CollUtil.isEmpty(list)){
            return new PageResult();
        }
        //2.提取数据列表中的好友id
        List<Long> userIds = CollUtil.getFieldValues(list, "friendId", Long.class);
        //3.调用UserInfoApi查询好友的用户详情
        UserInfo info = new UserInfo();
        info.setNickname(keyword);
        Map<Long,UserInfo> map = userInfoApi.findByIds(userIds,info);
        //4.构造vo对象
        List<ContactVo> vos = new ArrayList<>();
        for (Friend friend : list) {
            UserInfo userInfo = map.get(friend.getFriendId());
            if (userInfo != null){
                ContactVo vo = ContactVo.init(userInfo);
                vos.add(vo);
            }
        }
        return new PageResult(page,pagesize,0l,vos);
    }


}
